const Base = require('./base.js');
module.exports = class extends Base{
    indexAction(){
        return this.display();
    }

    async addAction(){
        const user = this.model('thinkjsplus_category');
        const data = await user.select();
        //获取分类数据
        this.assign('category',data);
        return this.display();
    }

    /**
     * 添加分类
     */
    async saveAction(){
        let data = this.post();
        console.log(data);
        if(think.isEmpty(data.id)){
            //保存到数据库
            let res = await this.model("thinkjsplus_thing").add(data);
            if(res){
                this.json({"success":true});
            }else{
                this.json({"success":false});
            }
        }else{
            //更新操作
            let res = await this.model("thinkjsplus_thing").update(data);
            if(res){
                this.json({"success":true});
            }else{
                this.json({"success":false});
            }
        }
    }

    /**
     * 查看事情列表
     */
    async listAction(){
        //实例化模型
        const user = this.model('thinkjsplus_thing');
        const data = await user.select();
        return this.json(data);
    }

    /**
     * 删除
     */
    async delAction(){
        let thingModel = this.model("thinkjsplus_thing");
        let posts = this.post("id");
        let delNums = thingModel.where({id:['IN',posts]}).delete();
        if(delNums){
            this.json({"success":true});
        }else{
            this.json({"success":false});
        }
    }
}